package com.liteng.shadow.service.group.impl;

import com.liteng.shadow.core.service.impl.BaseServiceImpl;
import com.liteng.shadow.core.utils.StringUtils;
import com.liteng.shadow.dao.group.IGroupFansDao;
import com.liteng.shadow.model.group.Group;
import com.liteng.shadow.model.group.GroupFans;
import com.liteng.shadow.model.member.Member;
import com.liteng.shadow.service.group.IGroupFansService;
import com.liteng.shadow.service.member.IMemberService;
import com.liteng.shadow.service.group.IGroupEmailService;
import com.liteng.shadow.utils.ConfigUtil;
import com.liteng.shadow.utils.EmailSendUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by wangyancheng on 2019/6/20.
 */
@Service("groupEmailService")
public class GroupEmailServiceImpl implements IGroupEmailService {
    private static final Logger logger = LoggerFactory.getLogger(GroupEmailServiceImpl.class);

    @Resource
    private IGroupFansService groupFansService;

    @Resource
    private IMemberService memberService;

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public boolean sendGroupEmail(Group group, String content, String subject, HttpServletRequest request) {
        final String account = (String) request.getServletContext().getAttribute(ConfigUtil.SITE_SEND_EMAIL_ACCOUNT.toUpperCase());
        final String passWord = (String) request.getServletContext().getAttribute(ConfigUtil.SITE_SEND_EMAIL_PASSWORD.toUpperCase());
        final String smtp = (String) request.getServletContext().getAttribute(ConfigUtil.SITE_SEND_EMAIL_SMTP.toUpperCase());

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Integer groupId = group.getId();
                    List<GroupFans> listFans = groupFansService.findByGroup(groupId);
                    if(null == listFans || listFans.isEmpty()){
                        return;
                    }
                    List<String> allFansAddress = new ArrayList<>();
                    for(GroupFans groupFans: listFans){
                        Member member = memberService.findById(groupFans.getMemberId());
                        String fanAddress = member.getEmail();
                        if(StringUtils.isEmpty(fanAddress)){
                            continue;
                        }
                        allFansAddress.add(fanAddress);
                    }
                    if(allFansAddress.isEmpty()){
                        return;
                    }



                    String address = String.join(",", allFansAddress);
                    logger.info("allFansAddress size  = {}", allFansAddress);
                    if(!EmailSendUtil.sendMail(account,passWord,smtp, allFansAddress, content, subject)){
                        for(int i =0;i<allFansAddress.size();i += 10){
                            int toIndex = i +10;
                            if (toIndex > allFansAddress.size()){
                                toIndex =  allFansAddress.size();
                            }
                            if(!EmailSendUtil.sendMail(account,passWord,smtp, allFansAddress.subList(i,toIndex), content, subject)){
                                for(int j=i;j< toIndex;j++){
                                    EmailSendUtil.sendMail(account,passWord,smtp, allFansAddress.get(j), content, subject);
                                }
                            }
                        }
                    }/*
                    for(String fansAddress: allFansAddress){
                        EmailSendUtil.sendMail(account,passWord,smtp, fansAddress, content, subject);
                    }*/
/*
                if(!EmailSendUtil.sendMailFans(request, address, content, subject)){
                    for(String fansAddress: allFansAddress){
                        EmailSendUtil.sendMailFans(request, fansAddress, content, subject);
                    }
                }*/
                    logger.info("address = {}", address);
                } catch (Exception e) {
                    logger.info("Exception = {}", e);
                }
            }
        });

        return true;
    }
}
